module enqueueMuxer32 (
    input wire clk,
    input wire rst,
    input wire enqueue_sucess,
    input wire queue_en_1,
    input wire queue_en_2,
    input wire queue_en_3,
    input wire queue_en_4,
    input wire queue_en_5,
    input wire queue_en_6,
    input wire queue_en_7,
    input wire queue_en_8,
    input wire queue_en_9,
    input wire queue_en_10,
    input wire queue_en_11,
    input wire queue_en_12,
    input wire queue_en_13,
    input wire queue_en_14,
    input wire queue_en_15,
    input wire queue_en_16,
    input wire [5:0] enqueue_priority_1,
    input wire [5:0] enqueue_priority_2,
    input wire [5:0] enqueue_priority_3,
    input wire [5:0] enqueue_priority_4,
    input wire [5:0] enqueue_priority_5,
    input wire [5:0] enqueue_priority_6,
    input wire [5:0] enqueue_priority_7,
    input wire [5:0] enqueue_priority_8,
    input wire [5:0] enqueue_priority_9,
    input wire [5:0] enqueue_priority_10,
    input wire [5:0] enqueue_priority_11,
    input wire [5:0] enqueue_priority_12,
    input wire [5:0] enqueue_priority_13,
    input wire [5:0] enqueue_priority_14,
    input wire [5:0] enqueue_priority_15,
    input wire [5:0] enqueue_priority_16,
    input wire [9:0] enqueue_index_1,
    input wire [9:0] enqueue_index_2,
    input wire [9:0] enqueue_index_3,
    input wire [9:0] enqueue_index_4,
    input wire [9:0] enqueue_index_5,
    input wire [9:0] enqueue_index_6,
    input wire [9:0] enqueue_index_7,
    input wire [9:0] enqueue_index_8,
    input wire [9:0] enqueue_index_9,
    input wire [9:0] enqueue_index_10,
    input wire [9:0] enqueue_index_11,
    input wire [9:0] enqueue_index_12,
    input wire [9:0] enqueue_index_13,
    input wire [9:0] enqueue_index_14,
    input wire [9:0] enqueue_index_15,
    input wire [9:0] enqueue_index_16,
    output reg enqueue_out,
    output reg [5:0] enqueue_priority_out,
    output reg [15:0] enqueue_value_out
);
    wire [7:0] arbitration_group [0:3];
    wire [3:0] enqueue_part;
    wire [5:0] enqueue_priority [0:3];
    wire [9:0] enqueue_index [0:3];
    wire [5:0] enqueue_where [0:3];

    wire [3:0] final_arbitration;
    reg delay;
    integer i;

    always @(posedge clk) begin
        if (rst) begin
            enqueue_out <= 1'b0;
            enqueue_priority_out <= 0;
            delay <= 0;
        end
        else begin
            if (enqueue_sucess || delay) begin
                enqueue_out <= 1'b0;
                enqueue_priority_out <= 0;
                delay <= 1;
                if (delay == 1) begin
                    delay <= 0;
                end
            end
            else begin
                if (final_arbitration == 4'b0001) begin
                    enqueue_out <= 1'b1;
                    enqueue_priority_out <= enqueue_priority[0];
                    enqueue_value_out <= ((enqueue_where[0]+16) << 10) + enqueue_index[0];
                end

                if (final_arbitration == 4'b0010) begin
                    enqueue_out <= 1'b1;
                    enqueue_priority_out <= enqueue_priority[1];
                    enqueue_value_out <= ((enqueue_where[1]+24) << 10) + enqueue_index[1];
                end

                if (final_arbitration == 4'b0000) begin
                    enqueue_out <= 1'b0;
                end
            end
        end
    end

    //定义仲裁选择器，作用是从16个信号里选中最紧急一组信号入队
    enqueueMuxer8 muxer1(.clk(clk), .rst(rst), .queue_en_1(queue_en_1), .enqueue_priority_1(enqueue_priority_1), .enqueue_index_1(enqueue_index_1),
                        .queue_en_2(queue_en_2), .enqueue_priority_2(enqueue_priority_2), .enqueue_index_2(enqueue_index_2),
                        .queue_en_3(queue_en_3), .enqueue_priority_3(enqueue_priority_3), .enqueue_index_3(enqueue_index_3),
                        .queue_en_4(queue_en_4), .enqueue_priority_4(enqueue_priority_4), .enqueue_index_4(enqueue_index_4),
                        .queue_en_5(queue_en_5), .enqueue_priority_5(enqueue_priority_5), .enqueue_index_5(enqueue_index_5),
                        .queue_en_6(queue_en_6), .enqueue_priority_6(enqueue_priority_6), .enqueue_index_6(enqueue_index_6),
                        .queue_en_7(queue_en_7), .enqueue_priority_7(enqueue_priority_7), .enqueue_index_7(enqueue_index_7),
                        .queue_en_8(queue_en_8), .enqueue_priority_8(enqueue_priority_8), .enqueue_index_8(enqueue_index_8),
                        .enqueue_arbitration(arbitration_group[0]), .enqueue_out(enqueue_part[0]), .enqueue_priority_out(enqueue_priority[0]), .enqueue_index_out(enqueue_index[0]), .enqueue_where_out(enqueue_where[0]));

    enqueueMuxer8 muxer2(.clk(clk), .rst(rst), .queue_en_1(queue_en_9), .enqueue_priority_1(enqueue_priority_9), .enqueue_index_1(enqueue_index_9),
                        .queue_en_2(queue_en_10), .enqueue_priority_2(enqueue_priority_10), .enqueue_index_2(enqueue_index_10),
                        .queue_en_3(queue_en_11), .enqueue_priority_3(enqueue_priority_11), .enqueue_index_3(enqueue_index_11),
                        .queue_en_4(queue_en_12), .enqueue_priority_4(enqueue_priority_12), .enqueue_index_4(enqueue_index_12),
                        .queue_en_5(queue_en_13), .enqueue_priority_5(enqueue_priority_13), .enqueue_index_5(enqueue_index_13),
                        .queue_en_6(queue_en_14), .enqueue_priority_6(enqueue_priority_14), .enqueue_index_6(enqueue_index_14),
                        .queue_en_7(queue_en_15), .enqueue_priority_7(enqueue_priority_15), .enqueue_index_7(enqueue_index_15),
                        .queue_en_8(queue_en_16), .enqueue_priority_8(enqueue_priority_16), .enqueue_index_8(enqueue_index_16),
                        .enqueue_arbitration(arbitration_group[1]), .enqueue_out(enqueue_part[1]), .enqueue_priority_out(enqueue_priority[1]), .enqueue_index_out(enqueue_index[1]), .enqueue_where_out(enqueue_where[1]));

    //第二层的四选一仲裁器
    priorityArbiter4 arb(.write_en_1(enqueue_part[0]), .priority_1(enqueue_priority[0]), .write_en_2(enqueue_part[1]), .priority_2(enqueue_priority[1]), .write_en_3(1'b0), .priority_3(6'b000000), .write_en_4(1'b0), .priority_4(6'b000000), .priority_arbitration(final_arbitration));
endmodule